<template>
  <c-select
    v-bind="$attrs"
    v-on="$listeners"
    :value="value"
    :options="options"
    @change="hangleChange"
    :props="props"
  ></c-select>
</template>

<script>
import { mapGetters } from 'vuex'
import { addressCut } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    value: String,
    filterHistory: {
      type: Boolean,
      default: false,
    },
    channelType: {
      type: String,
      default: '0',
    },
    rural: {
      type: String,
      default: '0',
    },
    guaType: {
      type: String,
      default: '',
    },
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data() {
    return {
      tableName: 'industrial_package_agency_view',
      loading: false,
      options: [],
      props: {
        label: 'load_name',
        value: 'industrial_package_id',
      },
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId']),
    bankType() {
      return this.user.clientOrgClassType
    },
    queryAddressCode() {
      return this.user.queryAddressCode
    },
  },
  watch: {},
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.loading = true
      this.$api
        .getData(
          this.$serviceName,
          this.tableName,
          {
            'agency_type_sub.eq': this.bankType,
            'orWhere.supply_address_code.endlike': addressCut(this.queryAddressCode),
            'orWhere.agency_id.eq': this.clientId,
            'channel_type.eq': this.channelType,
            'is_rural_dev.eq': this.rural,
            'gua_type.in': this.guaType,
            groupFields: 'industrial_package_id',
          },
          {
            create_time: 'desc',
          }
        )
        .then(res => {
          const data = res?.data ?? []
          if (this.filterHistory) {
            this.options = data.filter(item =>
              +item.gua_type === 3 ? +item.channel_type !== 0 : item
            )
          } else {
            this.options = data
          }
          if (this.value) {
            const select =
              this.options.find(item => item.industrial_package_id === this.value) || {}
            this.$emit('select', select.load_name, select)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    hangleChange(val) {
      const select = this.options.find(item => item.industrial_package_id === val) || {}
      this.$emit('select', select.load_name, select)
    },
  },
}
</script>

<style lang="scss" scoped></style>
